
const usedServer = /webpack-dev-server/.test(process.argv[1]);
const isDevelopment = (process.env.NODE_ENV || '').toLowerCase().substr(0, 3) === 'dev' || usedServer;

// 按需载入配置，参见 babel-plugin-import
const importOnDemand = {
//  antd: { 'libraryName': 'antd', 'libraryDirectory': 'es', 'style': 'css' },
};

module.exports = {
	presets: [
		!isDevelopment && ['@babel/preset-env', {
			targets: { chrome: 60 },
			corejs: 3,
			forceAllTransforms: true,
			useBuiltIns: 'entry',
			modules: false,
		}],
	].filter(Boolean),
	plugins: [
		...Object.keys(importOnDemand).map(it => ['babel-plugin-import', importOnDemand[it], it]),              // 按需导入
		'@babel/plugin-transform-react-jsx',                                                                    // JSX
		['@babel/plugin-transform-typescript', {isTSX: true}],                                                  // TypeScript
		'@babel/plugin-proposal-export-default-from',                                                           // export name from 'mod'
		'@babel/plugin-proposal-export-namespace-from',                                                         // export * as name from 'mod'
		'@babel/plugin-proposal-throw-expressions',                                                             // throw 表达式
		'@babel/plugin-proposal-do-expressions',                                                                // do 表达式
		'@babel/plugin-proposal-nullish-coalescing-operator',                                                   // 空结合运算符 ??
		'@babel/plugin-transform-exponentiation-operator',                                                      // 幂运算符     **
		'@babel/plugin-proposal-numeric-separator',                                                             // 数字分隔符    1_234_567
		'@babel/plugin-proposal-optional-chaining',                                                             // 链运算符     ?.
		'@babel/plugin-proposal-function-bind',                                                                 // 双冒号函数绑定运算符   this::f()  ::this.f
		['@babel/plugin-proposal-pipeline-operator', { proposal: 'minimal' }],                                  // 管道运算符  |>
		'@babel/plugin-proposal-optional-catch-binding',                                                        // try { } catch { }
		'@babel/plugin-syntax-dynamic-import',                                                                  // 异步导入    import()
		'@babel/plugin-transform-destructuring',                                                                // 展开运算    ...
		['@babel/plugin-proposal-class-properties', { oose: true } ],                                           // 类属性
		['@babel/plugin-proposal-object-rest-spread', { useBuiltIns: true } ],                                  // 对象展开
		['@babel/plugin-transform-runtime', { helpers: false, regenerator: true } ],
		['@babel/plugin-transform-regenerator', { async: false } ],
	].filter(Boolean),
};
